//
//  FlightInforCell.swift
//  ImGuider X
//
//  Created by 王鹏宇 on 9/29/18.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import CoreLocation

enum FlightType {
    
    case departure
    case destination
    case transfer
}

class FlightInforCell: UITableViewCell {
    
    @IBOutlet weak var flightNumDesLabel: UILabel!
    @IBOutlet weak var flightTypeDesLabel: UILabel!
    
    
    @IBOutlet weak var pointImageView: UIImageView!
    
    @IBOutlet weak var lineView: UIView!
    
    @IBOutlet weak var descLabel: UILabel!
    
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var airportNameLabel: UILabel!
    
    @IBOutlet weak var guidBtn: UIButton!
    
    @IBOutlet weak var inforView: UIView!
    
    @IBOutlet weak var flightNumLabel: UILabel!
    
    @IBOutlet weak var airTypeLabel: UILabel!
    
    @IBOutlet weak var descHeight: NSLayoutConstraint!
    
    @IBOutlet weak var inforViewHeight: NSLayoutConstraint!
    
    var openMaps:((CLLocationCoordinate2D?)->Void)?
    
    var coor:CLLocationCoordinate2D?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        flightNumDesLabel.text = LocalizedString("flight_no")
        flightTypeDesLabel.text = LocalizedString("flight_type")
        
        self.descLabel.textColor = kContentColor
        self.timeLabel.textColor = kContentColor
        self.airportNameLabel.textColor = kTitleColor
        
        self.flightNumLabel.textColor = kSubitleColor
        self.airTypeLabel.textColor = kSubitleColor
        
        self.guidBtn.addShadow(type: .btn)
        self.inforView.addShadow(type: .dayArrange)
        self.pointImageView.layer.cornerRadius = 4.0
        self.pointImageView.clipsToBounds = true
        self.pointImageView.backgroundColor = kContentColor
        
        self.contentView.backgroundColor = kThemeBgColor
        self.selectionStyle = .none
    }
    
    
    @IBAction func goGuide(_ sender: UIButton) {
        
        openMaps?(coor)
    }
    
    
    func configData(airDetail : AirplaneDetailModel?,model : Any?,type : FlightType){
        
        
        guard let airDetail = airDetail, let model = model else {
            
            return
        }
        switch type {
            
        case .departure:
            
            guard let airport = model as? AirportModel else { return }
            
            airportNameLabel.text = airport.airport_cname
            setupTimeLabel(airDetail.departureTime)
            descHeight.constant = 0
            descLabel.isHidden = true
            inforView.isHidden = false
            inforViewHeight.constant = 88
            
            if let airName = airDetail.airlineName,let flightNo = airDetail.flightNo {
                self.flightNumLabel.text = airName + " " + flightNo
            }
            
            if let flightDetails = airDetail.flightDetail,flightDetails.count > 0 {
                self.airTypeLabel.text = flightDetails[0].planeModel
            }
            
            
            coor = airport.coor
        case .destination:
            
            guard let airport = model as? AirportModel else { return }
            
            airportNameLabel.text = airport.airport_cname
            setupTimeLabel(airDetail.arriveTime,airDetail.departureTime)
            descHeight.constant = 30
            descLabel.isHidden = false
            descLabel.text = LocalizedString("arrive")
            inforView.isHidden = true
            inforViewHeight.constant = 0
            
//            coor = airport.coor
            
        case .transfer:
            
            guard let transer = model as? TransferModel, let airport = transer.departureAirportObj else { return }
            
            airportNameLabel.text = airport.airport_cname
            
            inforView.isHidden = false
            inforViewHeight.constant = 88
            if let airName = airDetail.airlineName,let flightNo = airDetail.flightNo {
                self.flightNumLabel.text = airName + " " + flightNo
            }
            
            self.airTypeLabel.text = transer.planeModel
            setupTimeLabel(transer.departureTime,airDetail.departureTime)
            
            descLabel.isHidden = false
            descLabel.text = LocalizedString("transition")
            
            coor = airport.coor
        }
        
        guidBtn.isHidden = (coor == nil)
    }
    
    /// 设置时间 如果传 originDate，表示需要处理时间（次日 当日）
    ///
    /// - Parameters:
    ///   - dateStr: 处理前的时间
    ///   - airDetail: 飞机信息
    private func setupTimeLabel(_ dateStr:String?,_ originDate:String? = nil) {
        
        if let departureDate = dateStr,
            let date = Date.date(departureDate, "yyyy-MM-dd HH:mm:ss") {
            
            
            var formatDate = date.string("MM-dd hh:mm aa",language: .chinese)
            
            if AppManager.share.language == .english {
                
                formatDate = date.string("dd MMM hh:mm aa",language: .english)
            }
            
            
            if
                let originDeparturestr = originDate,
                let originDate = Date.date(originDeparturestr, "yyyy-MM-dd HH:mm:ss") {
                
                var dateStr = date.string("hh:mm aa",language: .chinese)
                
                if AppManager.share.language == .english {
                    
                    dateStr = date.string("hh:mm aa",language: .english)
                }
                
                let components =
                    NSCalendar.current.dateComponents([.day], from: originDate, to: date)
                
                let day = components.day ?? -1
                
                if day == 0 {
                    
                    formatDate = "\(LocalizedString("today")) \(dateStr ?? "")"
                    
                } else if day == 1 {
                    
                    formatDate = "\(LocalizedString("next_day")) \(dateStr ?? "")"
                }
            }
            
            let att1 = NSMutableAttributedString(string: formatDate ?? "")
            
            
            att1.yy_font = kFont14
            att1.yy_color = UIColor.hexStringToColor(hexString: "#888888")
            self.timeLabel.attributedText = att1
        } else {
            
            self.timeLabel.text = "--"
        }
    }
    
}




